package udp2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPReceDemo {
	public static void main(String[] args) throws IOException {
		System.out.println("接收端启动");

		DatagramSocket ds = new DatagramSocket();
		while (true) {
			byte[] buf = new byte[1024];
			DatagramPacket dPacket = new DatagramPacket(buf, buf.length);

			ds.receive(dPacket);

			String ip = dPacket.getAddress().getHostAddress();
			int port = dPacket.getPort();
			String text = new String(dPacket.getData(), 0, dPacket.getLength());
			System.out.println(ip + ":" + port + ":" + text);
		}
	}
}
